<?php
namespace Admin\Controller;
use Think\Controller;
class RuleController extends CommonController 
{
    //rule 规则管理 显示
    public function index()
    {
      //个数
      $count = M('auth_rule')->count();
      $this->assign('num',$count);
      
    	//栏目规则分类 选择显示
        $sql = 'select * from auth_cate';
        $arr = M()->query($sql);
        $cate = $this->gettree($arr);
        $this->assign('cate',$cate);

        //规则显示
        $sql = 'select t1.*,t2.cname from auth_rule t1 inner join auth_cate t2 on t1.cid=t2.id';
        $rule = M()->query($sql);
        $this->assign('rule',$rule);

        $this->display();
    }

    //add 规则添加
    public function rule_add()
    {
    	//print_r($_POST);exit;
    	if (IS_POST) {
    		$data['cid'] = I('post.cateid');
    		$data['name'] = I('post.name');
    		$data['title'] = I('post.title');
    		$data['create_time'] = time();

        if (empty($data['cid'])) {
          $arr = array(
           "error" => 0,
          "info" => "请选择规则类"
              );
            echo json_encode($arr);
            exit;
        }

        $rulename = M('auth_rule')->where(array('name'=>$data['name']))->find();
        if ($rulename) {
          $arr = array(
            "error" => 0,
            "info" => "规则已存在"
              );
            echo json_encode($arr);
            exit;
        }
        $ruletitle = M('auth_rule')->where(array('title'=>$data['title']))->find();
        if ($ruletitle) {
          $arr = array(
            "error" => 0,
            "info" => "规则名称已存在"
              );
            echo json_encode($arr);
            exit;
        }

        $res = M('auth_rule')->add($data);
    		if ($res) {
	    		$arr = array(
				"error" => 1,
				"info" => "规则创建成功"
	        	);
	       	echo json_encode($arr);
    		}else {
				$arr = array(
					"error" => 0,
					"info" => "规则创建失败"
		        	);
		        echo json_encode($arr);
    		}

    	 }
    }

    //edit  编辑显示
    public function listedit()
    {
      $id = I('get.id');
      $sql = 'select * from auth_rule where id='.$id;
      $rule = M()->query($sql);
      $rule = current($rule);
      $this->assign('rule',$rule);

      //栏目选择显示
        $sql = 'select * from auth_cate';
        $arr = M()->query($sql);
        $cate = $this->gettree($arr);
        $this->assign('cate',$cate);

      $this->display();
    }
    //edit  编辑
    public function edit()
    {
      //print_r($_POST);
      //Array ( [id] => 8 [name] => admin/admin/addaa [title] => 管理员添加a [cid] => 20 )
      if (IS_POST) {
        $id = I('post.id');
        $data['name'] = I('post.name');
        $data['title'] = I('post.title');
        $data['cid'] = I('post.cid');
        $data['edit_time'] = time();

        $rule_row = M('auth_rule')->where(array('id'=>$id))->find();
        $ruleinfo = M('auth_rule')->select();
        foreach ($ruleinfo as $v) {
          if ($v['name']!=$rule_row['name'] && $v['name'] == $data['name']) {
              $arr = array(
                  'error' =>0,
                  'info'=>"权限规则名字已存在"
                  );
              echo json_encode($arr);
              exit;
          }
        }
        

        $res = M('auth_rule')->where(array('id'=>$id))->save($data);
        if ($res) {
            $arr = array(
                'error' =>1,
                'info'=>"规则修改成功"
                );
            echo json_encode($arr);
        }else {
            $arr = array(
                'error' =>0,
                'info'=>"规则修改失败"
                );
            echo json_encode($arr);
        }

      }

    }


    //彻底删除
    public function delete()
    {
       // print_r($_POST);exit;
      if (IS_POST) {
          $id = I('post.id');

          $sql = 'delete from auth_rule where id='.$id;
          $is_del = M()->execute($sql);
          if ($is_del) {
              $arr = array(
                  'error' =>0,
                  'info'=>"规则彻底删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"规则彻底删除失败"
                  );
              echo json_encode($arr);
          }
      }
        
    }

    //批量删除
    public function deletes()
    {
      //print_r($_POST);exit;
      //Array ( [arr] => Array ( [0] => 1 [1] => 2 ) )
      if (IS_POST) {
        $arr = I('post.arr');
        $ids = implode(",", $arr);

        $res = M('auth_rule')->delete($ids);
          if ($res) {
              $arr = array(
                  'error' =>0,
                  'info'=>"规则删除成功"
                  );
              echo json_encode($arr);
          }else {
              $arr = array(
                  'error' =>1,
                  'info'=>"规则删除失败"
                  );
              echo json_encode($arr);
          }
      }

    }


}